package com.guigu.serviceoss.component;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Map;

@Service
public class SmsUtlis {

    public CommonResponse send(Map<String, Object> param, String phone) {
        DefaultProfile profile = DefaultProfile.getProfile("default","LTAI4G2U3JvPhZfF4DpQYkkV","RGy2juIuAj6KHCP8gSOd5ZzzwrjOOD");
        IAcsClient client = new DefaultAcsClient(profile);
        // 设置相关参数
        CommonRequest request = new CommonRequest();
        //request.setProtocol(ProtocolType.HTTPS);
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");

        request.putQueryParameter("PhoneNumbers", phone);
        request.putQueryParameter("SignName", "我的硅谷在线教育网站");
        request.putQueryParameter("TemplateCode", "SMS_195570718");
        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(param));

        // 最终的发送
        try {
            CommonResponse response = client.getCommonResponse(request);
            return response;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
